﻿/* 
 * Copyright (c) 2009 Intel Corporation
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -- Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * -- Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * -- Neither the name of the Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE INTEL OR ITS
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * Contributor: Nathan Letwory
 */

using System;
using System.Runtime.InteropServices;

namespace VerseSharp
{
	// helper struct (union) for Get/PutReal64
	[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
	struct punt
	{
		[System.Runtime.InteropServices.FieldOffset(0)]
		public uint i;
		[System.Runtime.InteropServices.FieldOffset(4)]
		public uint i2;

		[System.Runtime.InteropServices.FieldOffset(0)]
		public double real;
	}

    public static class Utils
    {
		public static double GetReal64(byte[] source, ref int pos)
		{
			punt p;
			p.real = 0;
			p.i = DataConverter.BigEndian.GetUInt32(source, pos);
			pos += 4;
			p.i2 = DataConverter.BigEndian.GetUInt32(source, pos);
			pos += 4;
			return p.real;
		}

		public static void PutReal64(byte[] destination, ref int pos, double val)
		{
			punt p;
			p.i = p.i2 = 0;
			p.real = val;
			DataConverter.BigEndian.PutBytes(destination, pos, p.i);
			pos += 4;
			DataConverter.BigEndian.PutBytes(destination, pos, p.i2);
			pos += 4;
		}
        public static string GetString(byte[] source, int maxLen, ref int pos)
        {
            int strSize = maxLen - 1;

            // Find the null terminator
            for (int i = 0; i < maxLen; i++)
            {
                if (source[pos + i] == 0x00)
                {
                    strSize = i;
                    break;
                }
            }

			if ((pos - strSize) == 0)
			{
				pos += 1;
				return null;
			}
            // Convert the bytes to a string
            string str = System.Text.Encoding.UTF8.GetString(source, pos, strSize);
            pos += strSize + 1;
            return str;
        }

        public static void PutString(byte[] destination, string str, int maxLen, ref int pos)
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
            int dataLen = data.Length;

            // Check if this string fits in the given boundary
            if (data.Length > maxLen - 1)
                dataLen = maxLen - 1;

            // Write the string data
            Buffer.BlockCopy(data, 0, destination, pos, dataLen);
            pos += dataLen;
            // Write the null terminator
            destination[pos++] = 0x00;
        }
    }
}
